package cn.springBoot.controller;

import org.camunda.bpm.engine.HistoryService;
import org.camunda.bpm.engine.RuntimeService;
import org.camunda.bpm.engine.TaskService;
import org.camunda.bpm.engine.history.HistoricIdentityLinkLog;
import org.camunda.bpm.engine.task.IdentityLink;
import org.camunda.bpm.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ljx
 * @description:  任务相关操作
 * @class cn.springBoot.controller.TaskServiceController
 * @create date: 2021/9/24
 */
@RequestMapping("/task")
@RestController
public class TaskTestServiceController {
     @Autowired
     TaskService taskService;

     @Autowired
     HistoryService historyService;
 


    /** 
     * @description: 查找自己的任务 
     * @param:userId 
     * @author: ljx
     * @create date: 2021/9/24 
     * @version: V1.0 
     * @return: void 
     */
     
    @GetMapping("findMyTask")
    public void findMyTask(String userId) {
        List<Task> taskList = taskService.createTaskQuery().taskAssignee(userId).list();
        for (Task task : taskList) {
            System.out.println("#############");
            System.out.println(task.getId());
            System.out.println(task.getCreateTime());
            System.out.println(task.getPriority());
            System.out.println(task.getExecutionId());
            System.out.println("#############");
        }
    }
    /** 
     * @description: 组用户查找自己的任务 
     * @param:userId 
     * @author: ljx
     * @create date: 2021/9/27 
     * @version: V1.0 
     * @return: void 
     */
     
    @GetMapping("findUsersTask")
    public void findGroupTask(String userId) {
        //taskCandidateUser() 多人  taskCandidateGroup 根据用户组id 查询当前任务
        List<Task> taskList = taskService.createTaskQuery().taskCandidateUser(userId).list();
        for (Task task : taskList) {
            System.out.println("#############");
            System.out.println(task.getId());
            System.out.println(task.getCreateTime());
            System.out.println(task.getPriority());
            System.out.println(task.getExecutionId());
            System.out.println("#############");
        }
    }

   /** 
    * @description:  任务转让 
    * @param:taskId 
 * @Param: assignee 
    * @author: ljx
    * @create date: 2021/9/24 
    * @version: V1.0 
    * @return: void 
    */
    
    @GetMapping("/setAssignee")
    public void setAssignee(String taskId, String assignee) {
        //setAssignee会触发assignment类型的监听器
        taskService.setAssignee(taskId, assignee);
    }

 
    
    /** 
     * @description: 根据任务反向查询处理人
     * @param:
     * @author: ljx
     * @create date: 2021/9/24 
     * @version: V1.0 
     * @return: void 
     */
     @GetMapping("/getIdentityLinksForTask")
    public void getIdentityLinksForTask(String taskId) {

        List<IdentityLink> identityLinksForTask = taskService.getIdentityLinksForTask(taskId);
        for (IdentityLink identityLink : identityLinksForTask) {
            System.out.println("########");
            System.out.println(identityLink.getTaskId());
            System.out.println(identityLink.getProcessDefId());
            System.out.println(identityLink.getUserId());
            System.out.println("########");

        }
    }
    
    /** 
     * @description:  查询历史任务处理人
     * @param:
     * @author: ljx
     * @create date: 2021/9/24 
     * @version: V1.0 
     * @return: void 
     */

    @GetMapping("/createHistoricIdentityLinkLogQuery")
    public void createHistoricIdentityLinkLogQuery() {
        //查询历史权限表
        List<HistoricIdentityLinkLog> list = historyService.createHistoricIdentityLinkLogQuery()
                .list();
        for (HistoricIdentityLinkLog identityLinkLog : list) {
            System.out.println("###########");
            System.out.println(identityLinkLog.getOperationType());
            System.out.println(identityLinkLog.getUserId());
            System.out.println(identityLinkLog.getGroupId());
            System.out.println(identityLinkLog.getId());
            System.out.println("###########");
        }
    }
   
    /** 
     * @description: 完成当前任务 
     * @param:taskId 
     * @author: ljx
     * @create date: 2021/9/24 
     * @version: V1.0 
     * @return: void 
     */

    @GetMapping("/completeTask")
    public void completeTask(String taskId){
        Map<String, Object> paramMap=new HashMap<>();

        paramMap.put("approve",true);

        paramMap.put("userName","userName");
        paramMap.put("userId","userName");

        taskService.complete(taskId,paramMap);

        System.out.println("ok");
    }



    /**
     * @description:
     * 任务签收  组任务只能签收之后才能完成
     * 签收完成之后 只有签收人才能再次签收 其他签收人再次签收 报错
     * 签收完成之后 别的人就不会在查询出任务
     * @param:taskId
     * @Param: userId
     * @author: ljx
     * @create date: 2021/9/27
     * @version: V1.0
     * @return: void
     */

    @GetMapping("/claim")
    public  void claim(String taskId,String userId){

        taskService.claim(taskId,userId);
        System.out.println("ok");
    }

    /**
     * @description:  任务归还  任务归还之后 其他组成员可以进行签收
     * @param:taskId
     * @author: ljx
     * @create date: 2021/9/27
     * @version: V1.0
     * @return: void
     */

    @GetMapping("/claimNull")
    public  void claimNull(String taskId){

        taskService.claim(taskId,null);
        System.out.println("ok");
    }


    /**
     * @description:   添加处理人（未被领取任务）
     * @param:taskId
     * @Param: userId
     * @author: ljx
     * @create date: 2021/9/27
     * @version: V1.0
     * @return: void
     */

    @GetMapping("/addCandidateUser")
    public void candidateUser(String taskId,String userId){

        taskService.addCandidateUser(taskId,userId);
       //taskService.addCandidateGroup(taskId,groupId); 添加组
        System.out.println("ok");
    }

   /**
    * @description:   删除处理人（未被领取任务）
    * @param:taskId
    * @Param: userId
    * @author: ljx
    * @create date: 2021/9/27
    * @version: V1.0
    * @return: void
    */

    @GetMapping("/candidateUser")
    public void deleteCandidateUser(String taskId,String userId){

        taskService.deleteCandidateUser(taskId,userId);
        System.out.println("ok");
    }

   /**
    * @description:  根据进程实例id获取当前任务id（获取当前实例的任务进度）
    * @param:processInstanceId 进程实例id
    * @author: ljx
    * @create date: 2021/11/4
    * @version: V1.0
    * @return: void
    */

    @RequestMapping("/getActivityTask")
    public void getActivityTask(String processInstanceId){
        //processInstanceId 为条件 进程实例id 如果没有是全部

        List<Task> list = taskService.createTaskQuery().processInstanceId(processInstanceId)
                .active()
                .list();
        for (Task task:list){
            System.out.println("name:"+task.getName());
            System.out.println("id:"+task.getId());
            System.out.println("ProcessDefinitionId"+task.getProcessDefinitionId());
            System.out.println("taskDefinitionKey"+task.getTaskDefinitionKey());
            System.out.println("当前任务执行人:"+task.getAssignee());

        }

    }

}
